﻿using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Htx.Fur.Application.System.Dtos;
using Htx.Fur.Core.Entities;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Htx.Fur.Application
{
    public class SystemService : ISystemService, ITransient
    {
        private IRepository<UserInfo> _userRepo;
        public SystemService(IRepository<UserInfo> userRepo)
        {
            this._userRepo = userRepo;
        }

        public string GetDescription()
        {
            return "让 .NET 开发更简单，更通用，更流行。";
        }

        public UserDto GetUser()
        {
            var count = _userRepo.Count();
            var index = new Random().Next(1, count + 1);
            var user = _userRepo.FirstOrDefault(a => a.Id == index);
            return user.Adapt<UserDto>();
        }

        public UserDto CreateUser(UserInfo user)
        {
            var _repo = Db.GetRepository<UserInfo>();
            var proxy = _repo.InsertNow(user);
            return proxy.Entity.Adapt<UserDto>();
        }

        public async Task<List<UserDto>> GetUserListAsync()
        {
            //var _personRepo = _userRepo.Change<Person>();       // 切换数据库
            //_dicRepo.Sql();

            //var dd = "select * from userinfo".SqlQueries<UserInfo>();
            //var dd = "select * from userinfo".SqlQuery<UserInfo>();

            new UserInfo { Id = 1, Age = 22 }.UpdateNow(ignoreNullValues: true);

            var dd = _userRepo.Where(a => a.Id == 1).ToQueryString();

            var list = await _userRepo.Entities.ToListAsync();
            return list.Adapt<List<UserDto>>();
        }

    }
}